﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CondominioG1.Negocio;
using CondominioG1.Dominio;

namespace CondominioG1.Controllers
{
    public class ReservaController : Controller
    {

        //Dependencias
        public ReservaService ReservaService { get; set; }
        public AreaService AreaService { get; set; }
        public UsuarioService UsuarioService { get; set; }   
        //
        // GET: /Reserva/

        public ActionResult Index()
        {
            ICollection<Reserva> modelo = ReservaService.ListaReservasActualizada();
            return View(modelo);
        }

        //
        // GET: /Reserva/Details/5

        public ActionResult Details(String Numero)
        {
            Reserva modelo = ReservaService.ObtenerReserva(Int32.Parse(Numero));
            return View(modelo);
        }

        //
        // GET: /Reserva/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Reserva/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                ReservaService.RealizarReserva(DateTime.Parse(collection["Fecha"]),
                                               Int32.Parse(collection["Personas"]),
                                               (String)collection["Residente"],
                                               (String)collection["Area"],
                                               (String)collection["Comentario"]
                                             );
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {

                ModelState.AddModelError(String.Empty, "Error al registrar la reserva : " + e.Message);
                return View();
            }
        }
        
        //
        // GET: /Reserva/Edit/5

        public ActionResult Edit(String Numero)
        {
            Reserva modelo = ReservaService.ObtenerReserva(Int32.Parse(Numero));
            return View(modelo);
        }

        //
        // POST: /Reserva/Edit/5

        [HttpPost]
        public ActionResult Edit(String Numero, FormCollection collection)
        {
            try
            {
                ReservaService.ModificarReserva(DateTime.Parse(collection["Fecha"]),
                                            Int32.Parse(collection["Personas"]),
                                            (String)collection["Residente.Dni"],
                                            (String)collection["Area.Codigo"],
                                            (String)collection["Comentario"]
                                          );
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                ModelState.AddModelError(String.Empty, "Error al modificar la reserva : " + e.Message);
                return View();
            }
        }

        //
        // GET: /Reserva/Delete/5

        public ActionResult Delete(String Numero)
        {
            Reserva modelo = ReservaService.ObtenerReserva(Int32.Parse(Numero));
            return View(modelo);
        }

        //
        // POST: /Reserva/Delete/5

        [HttpPost]
        public ActionResult Delete(String Numero, FormCollection collection)
        {
            try
            {
                ReservaService.EliminarReserva(Int32.Parse(Numero));
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                ModelState.AddModelError(String.Empty, "Error al eliminar : " + e.Message);
                return View();
            }
        }
    }
}
